package com.apexsoft.assets.base.utils;

import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public final class TimeUtil {
	
	private static final Logger log = LoggerFactory
			.getLogger(TimeUtil.class);
	
	public static int compareRange(long curTimeInMillis, String hour) {
		String ss[] = hour.split("-");
		if (ss.length == 2) {
			if (compareTo(curTimeInMillis, ss[0]) < 0)
				return -1;
			if (compareTo(curTimeInMillis, ss[1]) > 0)
				return 1;
		}
		return 0;
	}

	public static int compareTo(long curTimeInMillis, String tmStr) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(curTimeInMillis);
		String[] s = tmStr.split(":");
		int[] field = { Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND };
		for (int n = 0; n < field.length; n++) {
			int val = 0;
			if (s.length > n) {
				val = NumberUtils.toInt(s[n]);
			}
			int nval = calendar.get(field[n]);
			if (nval > val)
				return 1;
			if (nval < val)
				return -1;
		}
		return 0;
	}

	public static boolean isBetween(long curTimeInMillis, String tmStr,long offsetFrom , long offsetTo) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(curTimeInMillis);
		String[] s = tmStr.split(":");
		int[] field = { Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND };
		for (int n = 0; n < field.length; n++) {
			int val = 0;
			if (s.length > n) {
				val = NumberUtils.toInt(s[n]);
			}
			calendar.set(field[n], val);
		}
		long offset = (curTimeInMillis - calendar.getTimeInMillis())/1000;
		if((0 - offset) <= offsetFrom && offset <= offsetTo )
			return true;
		return false;
	}
	
	public static int nextDay(int day, int shift) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(format.parse(String.valueOf(day)));
			calendar.add(Calendar.DATE, shift);
			Date nextDate = calendar.getTime();
			return Integer.parseInt(format.format(nextDate));
		} catch (ParseException e) {
			log.warn(e.getMessage(), e);
		}
		return 0;
	}
	
	public static int daysBetween(int bDate, int eDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(sdf.parse(String.valueOf(bDate)));
			long bTime = calendar.getTimeInMillis();
			calendar.setTime(sdf.parse(String.valueOf(eDate)));
			long eTime = calendar.getTimeInMillis();
			long between_days = (eTime - bTime) / (1000 * 3600 * 24);
			return Integer.parseInt(String.valueOf(between_days));
		} catch (Exception e) {
			log.warn(e.getMessage(), e);
		}
		return 0;
	}
	
	public static int getWorkingDay(int day) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(sdf.parse(String.valueOf(day)));
			if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
				return nextDay(day, 1);
			} else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
				return nextDay(day, 2);
			}
			return day;
		} catch (Exception e) {
			log.warn(e.getMessage(), e);
		}
		return 0;
	}
	
	public static int getCurrentDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return Integer
				.parseInt(sdf.format(new Date(System.currentTimeMillis())));
	}
	
	public static String getCurrentTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		return sdf.format(new Date(System.currentTimeMillis()));
	}
	
}
